import Vue from 'vue'
import { getUserInfo, login } from '@/api/base/user'
import {
  getAccessToken,
  removeAccessToken,
  setAccessToken
} from '@/utils/accessToken'
// import { resetRouter } from '@/router'
import defaultSettings, { title, tokenName } from '@/config'
import md5 from 'js-md5'
import { resetRouter } from '@/router'
const state = () => ({
  accessToken: getAccessToken(),
  username: '',
  name: '',
  cardId: '',
  userId: null,
  unitId: null,
  levelTwoCompangCode: null,
  roleId: null,
  endTime: null,
  avatar: '',
  gender: '',
  permissions: []
})
const getters = {
  accessToken: (state) => state.accessToken,
  username: (state) => state.username,
  name: (state) => state.name,
  phone: (state) => state.phone,
  avatar: (state) => state.avatar,
  cardId: (state) => state.cardId,
  userId: (state) => state.userId,
  unitId: (state) => state.unitId,
  roleId: (state) => state.roleId,
  gender: (state) => state.gender,
  permissions: (state) => state.permissions
}
const mutations = {
  setAccessToken(state, accessToken) {
    state.accessToken = accessToken
    setAccessToken(accessToken)
  },
  setUserId(state, userId) {
    state.userId = userId
  },
  setCardId(state, cardId) {
    state.cardId = cardId
  },
  setUnitId(state, unitId) {
    state.unitId = unitId
  },
  SET_COMPANGCODE: (state, levelTwoCompangCode) => {
    state.levelTwoCompangCode = levelTwoCompangCode
  },
  setUsername(state, username) {
    state.username = username
  },
  setName(state, name) {
    state.name = name
  },
  setPhone(state, phone) {
    state.phone = phone
  },
  setroleId(state, roleId) {
    state.roleId = roleId
  },
  setAvatar(state, avatar) {
    state.avatar = avatar
  },
  setEndTime(state, endTime) {
    state.endTime = endTime
  },
  setPermissions(state, permissions) {
    state.permissions = permissions
  },
  setGender(state, gender) {
    state.gender = gender
  }
}
const actions = {
  setPermissions({ commit }, permissions) {
    commit('setPermissions', permissions)
  },

  async login({ commit }, userInfo) {
    const { account, password } = userInfo
    const { data } = await login({
      account,
      password: md5(password)
    })
    const accessToken = data
    if (accessToken) {
      commit('setAccessToken', accessToken)
      const hour = new Date().getHours()
      const thisTime =
        hour < 8
          ? '早上好'
          : hour <= 11
            ? '上午好'
            : hour <= 13
              ? '中午好'
              : hour < 18
                ? '下午好'
                : '晚上好'
      Vue.prototype.$baseNotify(`欢迎登录${title}`, `${thisTime}！`)
    } else {
      Vue.prototype.$baseMessage(
        `登录接口异常，未正确返回${tokenName}...`,
        'error'
      )
    }
  },
  async getUserInfo({ commit }) {
    const { data } = await getUserInfo()
    if (!data) {
      Vue.prototype.$baseMessage('验证失败，请重新登录...', 'error')
      return false
    }
    console.log(data, 'userData')
    const { avatar, id, cardId, name, userName, unitId, gender, roleId, phone } = data
    if (userName) {
      commit('setUserId', id)
      commit('setCardId', cardId)
      commit('setUnitId', unitId)
      commit('setUsername', userName)
      commit('setName', name)
      commit('setPhone', phone)
      commit('setroleId', roleId)
      commit('setPermissions', [roleId])
      commit('setAvatar', avatar)
      commit('setGender', gender)
      return [roleId]
    } else {
      Vue.prototype.$baseMessage('用户信息接口异常', 'error')
      return false
    }
  },
  async logout({ dispatch }) {
    await dispatch('resetAccessToken')
    await resetRouter()
  },
  resetAccessToken({ commit }) {
    commit('setPermissions', [])
    commit('setAccessToken', '')
    localStorage.removeItem('validData')
    removeAccessToken()
  }
}
export default { state, getters, mutations, actions }
